//
//  DDMessageRootVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/6/28.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDMessageRootVC.h"
#import "GlobalDefine.h"
#import "DDReplyMeVC.h"
#import "DDAdoptedVC.h"
#import "DDAskVC.h"
#import "DDSystemVC.h"
#import "DDPushDB.h"
#import "JSBadgeView.h"
@interface DDMessageRootVC ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *datas;
@property (nonatomic,strong) NSArray *imageDatas;
@property (nonatomic,strong) NSArray *badges;
@end

@implementation DDMessageRootVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    
    NSInteger rowCount1 = [DDPushDB rowCountWithWhere:@"type=3 and unread=0"];
    NSInteger rowCount2 = [DDPushDB rowCountWithWhere:@"type=2 and unread=0"];
    NSInteger rowCount3 = [DDPushDB rowCountWithWhere:@"type=1 and unread=0"];
    NSInteger rowCount4 = [DDPushDB rowCountWithWhere:@"type=4 and unread=0"];
    
    _badges = @[[NSNumber numberWithInteger:rowCount1],[NSNumber numberWithInteger:rowCount2],[NSNumber numberWithInteger:rowCount3],[NSNumber numberWithInteger:rowCount4]];
    
    
    if (rowCount1 + rowCount2 + rowCount3 + rowCount4 == 0) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"removePushMessage" object:nil];
    }
    
    [self.tableView reloadData];
}

- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.tableView.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"消息", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
     self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width,0.f)];

}

- (void)createData
{
    _datas = @[@"回答我的",@"被采纳的",@"被追问的",@"系统消息"];
    _imageDatas = @[@"回答的",@"被采纳",@"被追问",@"系统消息"];
    
    
}

#pragma mark -UITableDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [_datas count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIImageView *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 9, 25, 25)];
    iconImageView.image = [UIImage imageNamed:_imageDatas[indexPath.row]];
    [cell.contentView addSubview:iconImageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([self xPostiong:iconImageView sp:20], 2, cell.frame.size.width, 40)];
    label.text = _datas[indexPath.row];
    label.font = [UIFont fontWithName:@"Arial" size:20.0];
    [cell.contentView addSubview:label];
    
    
    NSNumber *number =  _badges[indexPath.row];
    NSInteger count = [number integerValue];
    if (count > 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(self.tableView.frame.size.width - 60, 10, 24, 24)];
        [cell.contentView addSubview:view];
        JSBadgeView *badgeView = [[JSBadgeView alloc] initWithParentView:view alignment:JSBadgeViewAlignmentCenterRight];
        badgeView.badgeText = [NSString stringWithFormat:@"%d", count];
    }
    
    return cell;
}

-(float) xPostiong:(UIView *)view sp:(float)sp
{
    return view.frame.size.width+view.frame.origin.x + sp;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0)
    {
        DDReplyMeVC *replyMeVC = [[DDReplyMeVC alloc] initWithNibName:@"DDReplyMeVC" bundle:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ToDetailPage" object:replyMeVC];
    }
    else if(indexPath.row == 1)
    {
        DDAdoptedVC *adoptedVC = [[DDAdoptedVC alloc] initWithNibName:@"DDAdoptedVC" bundle:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ToDetailPage" object:adoptedVC];
    }
    else if(indexPath.row == 2)
    {
        DDAskVC *askVC = [[DDAskVC alloc] initWithNibName:@"DDAskVC" bundle:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ToDetailPage" object:askVC];
    }
    else if(indexPath.row == 3)
    {
        DDSystemVC *systemVC = [[DDSystemVC alloc] initWithNibName:@"DDSystemVC" bundle:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ToDetailPage" object:systemVC];
    }
}

@end
